﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AiLuWeb.WebSite.Code;
using AiLuWeb.Model;
using AiLuWeb.Business;
using Ajeelee.Core.Utils;
using System.Collections;

namespace AiLuWeb.WebSite.WebAdmin
{
    public partial class CategoryEdit : AuthPageBase
    {
        protected int id;
        protected int pid;
        protected NewsClass info = new NewsClass();
        INewsClassBusiness newsClassBll = new NewsClassBusinessFactory().CreateNewsClassBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                id = QueryParamInt32("id");
                pid = QueryParamInt32("pid");
                if (id > 0)
                {
                    info = newsClassBll.GetById(id);
                    if (info == null)
                    {
                        info = new NewsClass();
                        MessageBoxGo("不存在该分类", "Category.aspx?pid=" + pid);
                    }
                    pid = info.PId;
                }

                if (IsPost)
                {
                    // 更新
                    if (id > 0)
                    {
                        initModel();
                        newsClassBll.Update(info);

                        if (info.Path.Length > 0)
                        {
                            string p = info.Path.Substring(0, info.Path.IndexOf('>'));
                            if (p.IsNotNullAndEmpty() && p.ToInt() > 0)
                            {
                                pid = p.ToInt();
                            }
                        }
                        Function.RemoveCacheObject("/cache/newsclassall/");
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("Category.aspx?pid=" + pid);
                    }
                    else // 新建
                    {
                        initModel();
                        newsClassBll.Insert(info);

                        Hashtable ht = new Hashtable();
                        ht.Add(NewsClassHash.TopNum, 1);
                        IList<NewsClass> tempClassList = newsClassBll.GetAllNewsClass(ht);
                        if (tempClassList.Count > 0)
                        {
                            NewsClass newsClass = Utility.GetNewsClass().FirstOrDefault(c => c.Id == tempClassList[0].PId);
                            if (newsClass != null)
                            {
                                ht.Clear();
                                ht.Add(NewsClassHash.Path, newsClass.Path + tempClassList[0].Id + ">");
                                ht.Add(NewsClassHash.Id, tempClassList[0].Id);
                                newsClassBll.UpdateSomeVal(ht);
                            }
                        }
                        System.Threading.Thread.Sleep(500);
                        Function.RemoveCacheObject("/cache/newsclassall/");
                        Page_GoTo("Category.aspx?pid=" + pid);
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }
        public void initModel()
        {
            if (id <= 0)
            {
                info = new NewsClass();
            }
            if (info.Author.IsNullOrEmpty())
            {
                info.Author = User.Identity.Name;
                info.InIP = IPAddress;
            }
            info.Name = FormParam("mname");
            info.PId = FormParamInt32("pid");
            if (pid == 15)
            {
                info.PId = FormParamInt32("cateid");
            }

            NewsClass newsClass = Utility.GetNewsClass().FirstOrDefault(c => c.Id == info.PId);
            if (newsClass != null)
            {
                info.Path = newsClass.Path + info.Id + ">";
            }
        }
    }
}